<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\SubtypeRequest;
use App\Http\Controllers\Controller;
use App\Models\Subdevicetype;

class SubtypeController extends Controller
{
    /**
     * 显示子类列表.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(SubtypeRequest $request)
    {
        $perPage = $request->input('pageSize', 15);
		
		
        $query = Subdevicetype::query();


        $subid = $request->input('ts_typeid', '');
        if (!empty($subid)) {
            $query->where('ts_typeid', trim($subid));
        }
        
        $typeId = $request->input('dpt_id', '');

        $query->where('ts_Maintypeid', trim($typeId));
        
        $list = $query->get();

        return ['data' => $list];
    }

    /**
     * 保存子类.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(SubtypeRequest $request)
    {
        $data = $request->input();
        $ret = Subdevicetype::create($data);

        if (!$ret) {
            $this->error('添加失败');
        }
    }

    /**
     * 显示子类明细.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * 更新子类信息.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(SubtypeRequest $request, $id)
    {
        $info = Subdevicetype::findOrFail($id);

        $data = $request->input();
        $ret = $info->update($data);

        if (!$ret) {
            $this->error('更新失败');
        }
    }

    /**
     * 删除子类.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $subtype = Subdevicetype::findOrFail($id);
        if (!$subtype->delete()) {
            $this->error('删除失败');
        }
    }
}
